﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MWTWorkUnitMgmtTool.Services;
using StructureMap;
using System.IO;
using System.Data.OleDb;
using MWTWorkUnitMgmtToolFE.Models;
using MWTWorkUnitMgmtTool.BusinessEntities;

namespace MWTWorkUnitMgmtToolFE.Controllers
{
    public class MappingController : Controller
    {
        //
        // GET: /Mapping/

       // IMappingService mappingService = ObjectFactory.GetInstance<IMappingService>();
        IProductionWorkUnitService productionWorkUnitService = ObjectFactory.GetInstance<IProductionWorkUnitService>(); 

       // public ActionResult Index()
       // {
            
       //     return View();
       // }

        
       // public ActionResult Searcher(dataTableParamModel param)
       // {
       //     if (!(string.IsNullOrEmpty(param.BOMName)))
       //     {
       //         IEnumerable<Mapping> model = mappingService.GetMappingsByBOM(param.BOMName).Where(f => f.ItemName.StartsWith(param.BOMName));

       //         var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
       //         Func<Mapping, string> orderingFunction = (c => sortColumnIndex == 0 ? c.ItemCode : sortColumnIndex == 1 ? c.ItemName : sortColumnIndex == 2 ? c.LocationCode : c.WorkUnitCode);

       //         var sortDirection = Request["sSortDir_0"];

       //         if (sortDirection == "asc")
       //         {
       //             model = model.OrderBy(orderingFunction); 
       //         }
       //         else
       //         {
       //             model = model.OrderByDescending(orderingFunction); 
       //         }

       //         var finalModel = model.Select(f => new { f.WorkUnitCode, f.LocationCode, f.ItemCode, f.ItemName });
       //         var filteredModel = finalModel;
                            
       //         if (!string.IsNullOrEmpty(param.sSearch))
       //         {
       //             filteredModel = finalModel.Where(f => f.WorkUnitCode.Contains(param.sSearch)
       //                                                 || f.ItemCode.Contains(param.sSearch)
       //                                                 || f.ItemName.Contains(param.sSearch)
       //                                                 || f.LocationCode.Contains(param.sSearch));
       //         }

       //         var pagedData = filteredModel.Skip(param.iDisplayStart).Take(param.iDisplayLength);
                
       //         return Json(new
       //         {
       //             sEcho = param.sEcho,
       //             iTotalRecords = model.Count(),
       //             iTotalDisplayRecords = model.Count(),
       //             aaData = pagedData
       //         }, JsonRequestBehavior.AllowGet);

       //     }
       //     return null;
       // }

        public ActionResult AutoCompleteItems(string term)
        {
            var model = productionWorkUnitService.GetAllItems().Where(f => f.ItemName.Contains(term)).Select(f => new { label = f.ItemName });

            return Json(model, JsonRequestBehavior.AllowGet);
        }

       // public ActionResult Edit(int id)
       // {
       //     var model = mappingService.GetMappingsByID(id);
       //     return View(model);
       // }

       // [HttpPost]
       // public ActionResult Edit(int id, FormCollection collection)
       // {
       //     var model = mappingService.GetMappingsByID(id);

       //     if(TryUpdateModel(model))
       //         return RedirectToAction("Index");

       //     return View(model);
       // }

       //// [HttpPost]
       // public ActionResult FileUpload()
       // {
       //     string filePath = null;
       //     foreach (string inputTagName in Request.Files)
       //     {
       //         HttpPostedFileBase InFile = Request.Files[inputTagName]; 
       //         if(InFile.ContentLength > 0 && (Path.GetExtension(InFile.FileName) == ".xls" || Path.GetExtension(InFile.FileName) == ".xlsx" || Path.GetExtension(InFile.FileName) == ".xlsm"))
       //         {
       //             filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(InFile.FileName)); 
       //             if(System.IO.File.Exists(filePath))
       //             {
       //                 System.IO.File.Delete(filePath);
       //             }
       //             InFile.SaveAs(filePath);
       //         }

       //         if (filePath != null)
       //         {
       //             mappingService.UploadMappings(filePath);
       //         }
       //     }

       //     return View();
       // }
    }
}
